package cn.niit.edu.mobile.studyDemo.mapper;

import cn.niit.edu.mobile.studyDemo.dto.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.data.domain.Page;

import java.util.List;
@Mapper
public interface HeroMapper {

    @Select("SELECT * FROM type_info WHERE id = #{id}")
    TypeInfo getChiefType(Integer id);

    @Select("SELECT * FROM type_info WHERE id = #{id}")
    TypeInfo getMinorType(Integer id);

    /**
     *根据id获取英雄信息
     * @param id 英雄id
     * @return
     */
    @Select("SELECT * FROM hero_info WHERE id = #{id}")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getHeroInfo(Integer id);


    /**
     * 创建一个新皮肤
     * @param skinInfo 皮肤数据实体
     * @return
     */
    @Insert("INSERT INTO skin_info(skin_name,hero_id,is_default)VALUES(#{skinName}," +
            "#{hero.id},#{isDefault})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    Integer createSkin(SkinInfo skinInfo);

    /**
     * 更新指定英雄的次要分类
     * @param id 英雄ID
     * @param newTypeId 新的类型
     * @return
     */
    @Update("UPDATE hero_info SET minor_type = #{newTypeId} WHERE id = #{id}")
    Integer updateHeroType(@Param("id") Integer id,@Param("newTypeId") Integer newTypeId);

    /**
     * 统计各类型英雄数量，按类型id升序排列
     * @return
             */
    @Select("SELECT b.id,b.type_name,b.type_detail,COUNT(1) hero_count " +
            "FROM hero_info a,type_info b " +
            "WHERE a.chief_type = b.id OR a.minor_type = b.id " +
            "GROUP BY b.id,b.type_name,b.type_detail")
    List<HeroTypeData> getTypeCount();

    /**
     * 按分类名称查询英雄列表，分页返回
     * @param typeName 类型名称
     * @param start 页码
     * @param size 页长
     * @return
     */
    @Select("SELECT a.* " +
            "FROM hero_info a LEFT JOIN type_info b ON a.chief_type = b.id OR a.minor_type = b.id " +
            "WHERE type_name = #{typeName}" +
            "LIMIT #{start},#{size}")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    List<HeroInfo> getTypeName(@Param("typeName") String typeName, @Param("start") Integer start, @Param("size") Integer size);

    @Select("SELECT count(1) totalcount " +
            "FROM hero_info a LEFT JOIN type_info b ON a.chief_type = b.id OR a.minor_type = b.id " +
            "WHERE type_name = #{typeName}")
    Long getHeroTypeCount(String typeName);

    /**
     * 随机获取一个英雄队伍，战士
     * @return
     */
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"战士\" or type_name=\"坦克\") " +
            "ORDER BY RAND() " +
            "LIMIT 1")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
   HeroInfo getHeroTeam();

    /**
     * 法师
     * @return
     */
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"法师\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getMage();

    /**
     * 射手
     * @return
     */
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"射手\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getStriker();

    /**
     * 辅助
     * @return
     */
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"辅助\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getAuxiliary();

    /**
     * 刺客
     * @return
     */
    @Select("SELECT * " +
            "FROM hero_info " +
            "where chief_type = any (select id from type_info where type_name = \"刺客\" ) " +
            "ORDER BY RAND() " +
            "LIMIT 1;")
    @Result(column = "chief_type",property = "chiefType",one = @One(select = "getChiefType",fetchType = FetchType.EAGER))
    @Result(column = "minor_type",property = "minorType",one = @One(select = "getMinorType",fetchType = FetchType.EAGER))
    HeroInfo getAssassin();

}
